package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.commmon.R;
import org.example.dto.CombinationDto;
import org.example.pojo.Combination;
import org.example.service.CategoryService;
import org.example.service.CombinationMedicineService;
import org.example.service.CombinationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")

public class CombinationController {
    @Autowired
    private CombinationMedicineService combinationMedicineService;
    @Autowired
    private CombinationService combinationService;


    @Autowired
    private CategoryService categoryService;





    //组合的分页查询
//    http://localhost:8080/combination/page?page=1&pageSize=10
    @GetMapping("page")
    public R<Page> page(int page,int pageSize,String name){


        //数据返回给前端

        return  combinationService.getPage(page,pageSize,name);




    }


    /**
     * 新增组合
     *需要向组合表和套菜菜品表中添加数据
     */

    //添加菜品(在DishController)
    //http://localhost:8080/dish/list?categoryId=1413384954989060097



//    http://localhost:8080/combination
    @PostMapping
    //value ="combinationCache",allEntries = true
    //删除combinationCache分类下的所哟缓存
    @CacheEvict(value ="combinationCache",allEntries = true)
    public R<String> add(@RequestBody CombinationDto combinationDto){
        System.out.println(combinationDto);
//        System.out.println(madicine);
        combinationService.saveWithMedicine(combinationDto);

        return R.success("新增成功");
    }


    /**
     SetmealDto(combinationDishes=[SetmealDish(id=null, combinationId=null, medicineId=null, name=非甾体抗炎药, price=13800, copies=1, sort=null, createTime=null, updateTime=null, createUser=null, updateUser=null, isDeleted=null),
     SetmealDish(id=null, combinationId=null, medicineId=null, name=氨基糖苷类抗生素, price=11800, copies=1, sort=null, createTime=null, updateTime=null, createUser=null, updateUser=null, isDeleted=null)], categoryName=null)
     *
     */

    /**
     * 删除组合
     * 需要同时删除套菜表，组合菜品表
     *http://localhost:8080/combination?ids=1415580119015145474
     */
    @DeleteMapping

    //value ="combinationCache",allEntries = true
    //删除combinationCache分类下的所哟缓存
    @CacheEvict(value ="combinationCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        combinationService.deleteWithMedicine(ids);
        return R.success("删除成功");
    }


    /**
     * 启售/停售
     * http://localhost:8080/combination/status/0?ids=1683829486811938818
     */
    @PostMapping("/status/{status}")
    public R<String> closeOropenSetmeal(@RequestParam List<Long> ids,@PathVariable Integer status){


        for (int i = 0; i < ids.size(); i++) {
            Combination combination = combinationService.getById(ids.get(i));
            combination.setStatus(status);
            combinationService.updateById(combination);
        }
        return R.success("成功");
    }


//
//    修改页面回显
//    http://localhost:8080/combination/1683832886639284225
    @GetMapping("/{id}")
    public R<CombinationDto> get(@PathVariable Long id){
        return  R.success(combinationService.getDtoById(id));
    }


    /**
     * 修改/更新
     * 对两张表的数据更新
     * combination
     * combinationDish
     * http://localhost:8080/combination
     */


    @PutMapping
    public R<String> update(@RequestBody CombinationDto combinationDto){
        combinationService.updateWithCombinationMedicine(combinationDto);
        return  R.success("成功");
    }



//    http://localhost:8080/combination/list?categoryId=1413342269393674242&status=1
        @GetMapping("/list")
        //将数据缓存到combinationCache这个类别，以categoryId_status的方式存储
        @Cacheable(value="combinationCache",key="#combination.categoryId+'_'+#combination.status")

        public R<List<Combination>> list(Combination combination){
            LambdaQueryWrapper<Combination> combinationLambdaQueryWrapper = new LambdaQueryWrapper<>();
            combinationLambdaQueryWrapper
                    .eq(Combination::getCategoryId, combination.getCategoryId())
                    .eq(Combination::getStatus, combination.getStatus());
            List<Combination> list = combinationService.list(combinationLambdaQueryWrapper);


            return R.success(list);

        }















}
